package com.example.strictmodedemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        networkButton.setOnClickListener {
            postNetwork()
        }
    }

    /**
     * 网络连接的操作
     */
    private fun postNetwork() {
        try {
            val url = URL("https://www.baidu.com")
            val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
            conn.connect()
            val reader = BufferedReader(InputStreamReader(conn.inputStream))
            var lines: String? = null
            val sb = StringBuffer()
            while (reader.readLine().also { lines = it } != null) {
                sb.append(lines)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}